;$(function () {
	var serverIdArr=[];
	var id = $("#id").val();
	if(id==null||id==''){
		upload(serverIdArr)
    }else{
    	getInfo(id,serverIdArr);	
    }
    contribute_validator();
});

var tale = new $.tale();//弹框

function upload(serverIdArr) {
    $("#dropzone").dropzone({
        paramName: "file",
        url: ctxPath+"/file/upload",
        addRemoveLinks: true,
        dictRemoveLinks: "x",
//        uploadMultiple: true,
        maxFilesize: 10,
        init: function () {
            this.on("success", function (file, msg) {
                file.serverId = msg.message;
                serverIdArr.push(msg.message);
                $("#image_files").val(serverIdArr);
            });
            this.on('error', function (files, response) {
                //文件上传失败后的操作
                console.log(files, response);
            });
            //删除图片的事件，当上传的图片为空时，使上传按钮不可用状态
            this.on("removedfile", function (file) {
                serverIdArr.splice($.inArray(file.serverId, serverIdArr), 1);
                $("#image_files").val(serverIdArr);
            });
        }
    });
}


function contribute_validator() {
    $("#global-form").validator({
        fields: {
            title: {
                rule: "required;",
                msg: {
                    required: "必填!"
                }
            },
            registrationDeadline: {
                rule: "required;",
                msg: {
                    required: "必填!"
                }
            },
            startTime: {
                rule: "required;",
                msg: {
                    required: "必填!"
                }
            },
            endTime: {
                rule: "required;",
                msg: {
                    required: "必填!"
                }
            },
            site: {
                rule: "required;",
                msg: {
                    required: "必填!"
                }
            },
            totalUsers: {
                rule: "required;",
                msg: {
                    required: "必填!"
                }
            }
        },
        invalid: function (form, errors) {
            return;
        },
        //验证成功
        valid: function (form) {
            saveInfo();
        },
    });
}


function getInfo(id){
    $.ajax({
        url: ctxPath+"/party/event/findById/"+id,
        type: 'GET',
        success: function(d){
            var success = d.success;
            if(success==true){
                var data=  d.data;
                umbinding.bind(data);
                ue.ready(function(){//监听编辑器实例化完成的事件
                    if(data.basicFunction!=null){
                        ue.setContent(data.basicFunction);
                    }
                });
                serverIdArr=data.fileId;
           	 $("#image_files").val(serverIdArr);
           	 var myDropzone = new Dropzone("#dropzone", {
           	        paramName: "file",
           	        url: ctxPath+"/file/upload",
           	        addRemoveLinks: true,
           	        dictRemoveLinks: "x",
//           	        uploadMultiple: true,
           	        maxFiles: 3,
           	        maxFilesize: 10,
           	        init: function () {
           	            this.on("success", function (file, msg) {
           	               	file.serverId=msg.message;
           	               	serverIdArr=msg.message;
           	            	$("#image_files").val(serverIdArr);
           	            });
           	            this.on('error', function (files, response) {
           	            });
           	            //删除图片的事件，当上传的图片为空时，使上传按钮不可用状态
           	            this.on("removedfile", function (file) {
           	            	$("#image_files").val("");
           	            });
           	        }
           	    });
           	 if(data.files!=null){
           	  for (var i = 0; i < data.files.length; i++) {
           		   var size = data.files[i].size*1024;
           			 var mockFile = { 
           					 name: data.files[i].srcname,
           					 accepted:true,
           					 size: size, //图片尺寸
       					     height:"70%",
       					     width:"70%",
       					     type:'.jpg,.png,.gif'//图片文件类型
           					    };
                   	 myDropzone.emit("addedfile", mockFile);
                   	 myDropzone.emit("thumbnail", mockFile, ctxPath+"/file/showImage/"+data.files[i].id);
                   	 myDropzone.emit("complete", mockFile);	
                   	 myDropzone.options.maxFiles = myDropzone.options.maxFiles - 1;
   				} 
           	 }
            }
        }
    });
}

function saveInfo() {
    var json =$("#global-form").serializeFormJSON();
    $.ajax({
        url: ctxPath+"/party/event/saveEventInfo",
        type: 'POST',
        data: json,
        success: function (d) {
            var success = d.success;
            if (success) {
                tale.alertOk({
                    text: '活动保存成功',
                    then: function () {
                        setTimeout(function () {
                            window.location.href = ctxPath+'/party/event/list';
                        }, 500);
                    }
                });
            } else {
                tale.alertError(result.message || '保存失败');
            }
        }
    });
}
